<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/1
 * Time: 16:48
 */
class Bus extends Controller{
    /*
     * 车辆
     * 数据列表
     */
    public function ListItem(){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        $where = '1=1';
        if(!empty($get_keyword->text)){
            $where .= ' and driver_name = "' . $get_keyword->text.'" or bus_number = "' . $get_keyword->text.'" or vehicle_device_id = "'. $get_keyword->text.'"';
        }
        _Db::Validates($request_data);
        $param_page = $get_page = input('Page', 1);
        $param_pagesize = 10;
        $result = Db::table('bus_info')
            ->field(
                [
                    'a.bus_id'           => 'Id',           //id
                    'a.vehicle_type'     => 'VehicleType',  //车辆型号
                    'a.bus_number'       => 'BusNumber',    //车牌号
                    'a.bus_status'       => 'Status',       //车辆状态
                    'a.vehicle_device_id'=> 'DeviceId',     //车载设备代码
                    'a.driver_name'      => 'DriverName',   //司机姓名
                    'a.driver_tel'       => 'DriverTel',    //司机电话
                    'a.bus_seat'         => 'Seat',         //座位数量
                    'a.oper_date'        => 'Date',         //操作日期
                    'd.line_from'        => 'LineFrom',
                    'd.line_destination' => 'LineDestination',
                ]
            )
            ->alias('a')
            ->join('line d','a.line_id = d.line_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('a.oper_date desc')
            ->select();
        foreach ( $result as $key => $value){
            if ($result[$key]['Status'] == 1){
                $result[$key]['Status'] = false;
            }elseif ($result[$key]['Status'] == 2){
                $result[$key]['Status'] = true;
            };
            $result[$key]['BusStatus'] = $value['Status'];
            $result[$key]['Order'] = $key + 1;
        };
        $int_total_count = count(Db::table('bus_info')
            ->alias('a')
            ->join('line d','a.line_id = d.line_id','LEFT')
            ->where($where)
            ->order('a.oper_date desc')
            ->select());
        $data['tableData']=$result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $itemid = new RequestEntity('ItemId', 'Id', true, 0)
        ];
        _Db::Validates($request_data);
        $param_where = [
            'a.bus_id' => $itemid->text,
        ];
        $result = Db::table('bus_info')
            ->field(
                [
                    'a.bus_id'           => 'Id',
                    'a.vehicle_type'     => 'VehicleType',
                    'a.bus_number'       => 'BusNumber',
                    'a.bus_status'       => 'BusStatus',
                    'a.vehicle_device_id'=> 'DeviceId',
                    'a.driver_name'      => 'DriverName',
                    'a.driver_tel'       => 'DriverTel',
                    'a.bus_seat'         => 'Seat',
                    'a.oper_date'        => 'Date',
                    'c.nick_name'        => 'OperName',
                    'd.line_from'        => 'LineFrom',
                    'd.line_destination' => 'LineDestination',
                ]
            )
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->join('line d','a.line_id = d.line_id','LEFT')
            ->where($param_where)
            ->find();
        $result['Line'] = $result['LineFrom'].'--'.$result['LineDestination'];
        return json_encode($result,true);
    }
    /*
     * 线路查询
     */
    public function FindLine(){
        $result = Db::table('line')
            ->field(
                [
                    'line_id'           => 'Id',
                    'line_from'         => 'From',                      //起点
                    'line_destination'  => 'Destination',               //终点
                ]
            )
            ->select();
        foreach ( $result as $key => $value){
            $result[$key]['Line'] = $value['From'].'--'.$value['Destination'];
        };
        return json_encode($result,true);
    }
    /*
     * 修改数据
     */
    public function ModifyItem(){
        $post_itemid = new RequestEntity('ItemId', 'ID', true, 0);
        $post_busNumber = new RequestEntity('BusNumber', '车牌号', true, 50);
        $post_deviceId = new RequestEntity('DeviceId','车载设备代码', false, 100);
        //车牌号校验
        $busNumber = _Db::GetData('bus_info','bus_number','bus_id !='.$post_itemid->text.' and bus_number = "'.$post_busNumber->text.'"');
        //车载设备代码校验
        $deviceId = _Db::GetData('bus_info','vehicle_device_id','bus_id !='.$post_itemid->text.' and vehicle_device_id = "'.$post_deviceId->text.'"');
        if (!empty($busNumber)){
            _Db::Response(2,'车牌号已存在');
        }elseif (!empty($deviceId)){
            _Db::Response(2,'车载设备代码已存在');
        }else{
            //获取请求参数
            $request_data = [
                $post_lineid = new RequestEntity('LineId', '线路ID', true, 0),
                $post_vehicleType = new RequestEntity('VehicleType', '车辆型号', true, 30),
//                $post_driverName = new RequestEntity('DriverName','司机姓名', false, 30),
//                $post_driverTel = new RequestEntity('DriverTel','司机电话', false, 30),
//                $post_seat = new RequestEntity('Seat','座位数', false, 30),
                $post_status = new RequestEntity('BusStatus','车辆状态', false, 30),
                $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
            ];
            //验证表单内容
            _Db::Validates($request_data);
            $res = array(
                'line_id'          => $post_lineid->text,
                'vehicle_type'     => $post_vehicleType->text,
                'bus_number'       => $post_busNumber->text,
                'vehicle_device_id'=> $post_deviceId->text,
//                'driver_name'      => $post_driverName->text,
//                'driver_tel'       => $post_driverTel->text,
//                'bus_seat'         => $post_seat->text,
                'bus_status'       => $post_status->text,
                'oper_id'          => $post_creatid->text,
                'oper_date'        => date('Y-m-d H:i:s',time()),
            );
            $param_where = [
                'bus_id' => $post_itemid->text,
            ];
            $result = Db::table('bus_info')
                ->where($param_where)
                ->update($res);
            if ($result){
                _Db::log($post_creatid->text,3,'车辆修改');
                $this->Item($param_where);
            };
        }
    }
    /*
     * 保存数据
     */
    public function AddItem(){
        $post_busNumber = new RequestEntity('BusNumber', '车牌号', true, 50);
        $post_deviceId = new RequestEntity('DeviceId','车载设备代码', false, 100);
        //车牌号校验
        $busNumber = _Db::GetData('bus_info','bus_number','bus_number = "'.$post_busNumber->text.'"');
        //车载设备代码校验
        $deviceId = _Db::GetData('bus_info','vehicle_device_id','vehicle_device_id = "'.$post_deviceId->text.'"');
        if (!empty($busNumber)){
            _Db::Response(2,'车牌号已存在');
        }elseif (!empty($deviceId)){
            _Db::Response(2,'车载设备代码已存在');
        }else{
            //获取请求参数
            $request_data = [
                $post_lineid = new RequestEntity('LineId', '线路ID', true, 0),
                $post_vehicleType = new RequestEntity('VehicleType', '车辆型号', true, 30),
//                $post_driverName = new RequestEntity('DriverName','司机姓名', false, 30),
//                $post_driverTel = new RequestEntity('DriverTel','司机电话', false, 30),
//                $post_seat = new RequestEntity('Seat','座位数', false, 30),
                $post_status = new RequestEntity('BusStatus','车辆状态', false, 30),
                $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
            ];
            _Db::Validates($request_data);
            $param_field = [
                'line_id'          => $post_lineid->text,
                'vehicle_type'     => $post_vehicleType->text,
                'bus_number'       => $post_busNumber->text,
                'vehicle_device_id'=> $post_deviceId->text,
//                'driver_name'      => $post_driverName->text,
//                'driver_tel'       => $post_driverTel->text,
//                'bus_seat'         => $post_seat->text,
                'bus_status'       => $post_status->text,
                'oper_id'          => $post_creatid->text,
                'oper_date'        => date('Y-m-d H:i:s',time()),
            ];
            $res = Db::table('bus_info')->insert($param_field);
            if ($res){
                _Db::log($post_creatid->text,1,'车辆添加');
                $this->ListItem();
            }
        }
    }
    //是否投放
    public function SwitchModity(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 10),
            $post_value = new RequestEntity('Value', '值', true, 10),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_field = '';
        if ($post_value->text == 1){
            $type = 6;
            $content = '车辆停用';
            $param_field = [
                'bus_status'    => 1,
                'oper_id'               => $post_creatid->text,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }elseif ($post_value->text == 2){
            $type = 5;
            $content = '车辆投放';
            $param_field = [
                'bus_status'    => 2,
                'oper_id'               => $post_creatid->text,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }
        //设置条件
        $param_where = [
            'bus_id' => $post_itemid->text,
        ];
        $result = Db::table('bus_info')
            ->where($param_where)
            ->update($param_field);
        if ($result){
            _Db::log($post_creatid->text,$type,$content);
            $this->Item($param_where);
        }
    }
    /*
     * 删除表数据
     */
    public function RemoveItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30)
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_where = [
            'bus_id' => $post_itemid->text,
        ];
        $result = Db::table('bus_info')
            ->where($param_where)
            ->delete();
        if ($result){
            _Db::log($post_creatid->text,1,'车辆删除');
            $this->ListItem();
        };
    }
    public function Item($param_where){
        $result = Db::table('bus_info')
            ->field(
                [
                    'a.bus_id'           => 'Id',           //id
                    'a.vehicle_type'     => 'VehicleType',  //车辆型号
                    'a.bus_number'       => 'BusNumber',    //车牌号
                    'a.bus_status'       => 'Status',    //车辆状态
                    'a.vehicle_device_id'=> 'DeviceId',     //车载设备代码
//                    'a.driver_name'      => 'DriverName',   //司机姓名
//                    'a.driver_tel'       => 'DriverTel',    //司机电话
                    'a.bus_seat'         => 'Seat',         //座位数量
                    'a.oper_date'        => 'Date',         //操作日期
                    'd.line_from'        => 'LineFrom',
                    'd.line_destination' => 'LineDestination',
                ]
            )
            ->alias('a')
            ->join('line d','a.line_id = d.line_id','LEFT')
            ->where($param_where)
            ->order('a.oper_date desc')
            ->find();
        $result['BusStatus'] = $result['Status'];
        if ($result['Status'] == 1){
            $result['Status'] = false;
        }elseif ($result['Status'] == 2){
            $result['Status'] = true;
        };
        _Db::ResponseJson($result);
    }
}